<?php

namespace app\index\controller;

use app\common\model\User;
use houdunwang\crypt\Crypt;
use think\Controller;
use think\Loader;
use think\Session;

/**
 * 前台登录 注册
 * Class Cart
 * @package app\index\controller
 */
class Login extends Controller
{
    public function login()
    {
        if(request()->isPost()){
            //实例化Admin模型，使用login方法获取所有post数据
            $res = (new User())->login(input('post.'));
            if($res['valid'])
            {
                //说明登录成功
                $this->success($res['msg'],'index/index/index');die;
            }else{
                //说明登录失败
                $this->error($res['msg']);die;
            }
        }
        return $this->fetch('login');
    }

    public function reg()
    {//注册操作
        if (request()->isPost()) {
            $_POST['password'] = Crypt::encrypt($_POST['password']);
            //实例化Admin模型，使用login方法获取所有post数据
            $res = (new User())->reg($_POST);
            if ($res['valid']) {
                //说明登录成功
                $this->success($res['msg'], 'index/login/login');
                die;
            } else {
                //说明登录失败
                $this->error($res['msg']);
                die;
            }
        }
        //1.加载模板
        return $this->fetch('reg');
    }
    //退出
    public function out()
    {
        Session::delete('user');
        $this->success('退出成功',url('index/index/index'));die;//跳转提示
    }

}
